<template>
  <el-dialog
    title="新增角色"
    v-model="dialogVisible"
    width="30%"
    :before-close="handleClose"
  >
    <el-form
      :model="roleForm"
      status-icon
      :rules="rules"
      ref="role-Form"
      label-width="100px"
      class="demo-roleForm"
    >
      <el-form-item label="角色名称" prop="rolename">
        <el-input
          v-model="roleForm.rolename"
          autocomplete="off"
          placeholder="请输入角色名称"
        ></el-input>
      </el-form-item>
      <el-form-item label="访问权限">
        <el-tree
          ref="menusTree"
          :data="menus.menuList"
          show-checkbox
          :default-checked-keys="menuArr"
          node-key="id"
          :props="defaultProps"
        >
        </el-tree>
      </el-form-item>
      <el-form-item label="角色状态">
        <el-switch
          v-model="roleForm.status"
          active-color="#13ce66"
          inactive-color="#ff4949"
          :active-value="1"
          :inactive-value="2"
        >
        </el-switch>
      </el-form-item>
    </el-form>
    <template #footer>
      <span class="dialog-footer">
        <el-button @click="closeDialog()">取消</el-button>
        <el-button type="primary" @click="submitRoles()"> 提交 </el-button>
      </span>
    </template>
  </el-dialog>
</template>

<script setup>
import { ref, reactive, onMounted } from "vue";
import { ElMessageBox, ElMessage } from "element-plus";
// 导入store
import useMenuStore from "../../stores/menus.js";
import useRoleStore from "../../stores/roles.js";
// 声明变量
const menus = useMenuStore();
const roles = useRoleStore();

onMounted(() => {
  // 调用函数,获取菜单数据
  menus.getMenuListActions();
});
// 定义控制弹窗显隐的变量
const dialogVisible = ref(false);
//关闭弹窗？
const handleClose = (done) => {
  ElMessageBox.confirm("确定关闭吗?", "关闭弹窗！", {
    confirmButtonText: "确定",
    cancelButtonText: "取消",
    type: "warning",
  })
    .then(() => {
      done();
      //   重置表单
      resetForm();
    })
    .catch(() => {
      // catch error
    });
};
// 关闭弹窗
const closeDialog = () => {
  resetForm();
};

// 定义角色表单
let roleForm = reactive({
  rolename: "",
  menus: "",
  status: 1, //1启用2禁用
});
// 定义编辑id
let editId = ref("");

let menuArr = reactive([]); //默认勾选的菜单选项
// 获取属性菜单的值
const menusTree = ref(null);

// 树形菜单
const defaultProps = reactive({
  children: "children",
  label: (menu) => {
    return menu.status !== 1 ? menu.title + "[已禁用]" : menu.title;
  },
  disabled: (menu) => {
    return menu.status !== 1;
  },
});

// 表单规则
const rules = reactive({
  rolename: {
    required: true,
    message: "请填写角色名称",
    trigger: "blur",
  },
  status: { required: true, message: "请选择角色状态", trigger: "blur" },
});

// 清空表单，关闭弹窗
const resetForm = () => {
  // 重置编辑id
  editId = ref("");
  // 初始化表单
  roleForm = reactive({
    rolename: "",
    menus: "",
    status: 1, //1启用2禁用
  });
  //   清空树形数组
  menuArr = reactive([]);
  //   清空树形后台数据
  menusTree.value.setCheckedKeys([]);
  // 关闭弹窗
  dialogVisible.value = false;
};

// 提交表单
const submitRoles = () => {
  //   console.log("roleForm", roleForm);
  roleForm.menus = menusTree.value.getCheckedKeys().join(",");
  if (editId.value) {
    roles.editRoleListActions({ ...roleForm, id: editId.value });
  } else {
    roles.addRoleListActions(roleForm);
  }

  resetForm();
};

// 接收父组件数据------编辑
const acceptValue = (value) => {
  //   console.log("value", value);
  editId.value = value.id;
  menuArr = value.menus.split(",");
  roleForm = reactive({ ...value });
};

// 将变量与函数暴露出来,此代码片段放到下面
defineExpose({
  dialogVisible,
  acceptValue,
});
</script>

<style lang="css" scoped></style>
